package com.hsnn.medstgmini.base.std.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.annotations.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.std.model.Classify;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.base.std.model.StdCompinfoLog;
import com.hsnn.medstgmini.base.std.service.StdCompinfoDetailManager;
import com.hsnn.medstgmini.base.std.service.StdCompinfoManager;
import com.hsnn.medstgmini.util.Pagination;
/**
 * 企业库 信息详情 控制层
 * @author 韩守松
 * 2015年6月10日
 */
@Controller
@RequestMapping("/stdCompinfoDetail")
public class StdCompinfoDetailController {

	@Source
	private StdCompinfoDetailManager StdCompinfoDetailManager;

	@Source
	private StdCompinfoManager stdCompinfoManager;
	
	@Autowired
	public void setStdCompinfoDetailManager(
			StdCompinfoDetailManager stdCompinfoDetailManager) {
		StdCompinfoDetailManager = stdCompinfoDetailManager;
	}
	
	@Autowired
	public void setStdCompinfoManager(StdCompinfoManager stdCompinfoManager) {
		this.stdCompinfoManager = stdCompinfoManager;
	}
	
	@Autowired
	private HttpServletRequest request;
	
	private static final Logger log = Logger.getLogger(StdCompinfoController.class);

	@RequestMapping(value="/stdCompinfoDetailListJsp",method={RequestMethod.GET,RequestMethod.POST})
	public String stdCompinfoDetailListJsp(HttpServletRequest request,Pagination page){
		printParams();
		StdCompinfoLog stdCompinfoLog =new StdCompinfoLog();
		String compId = request.getParameter("compId");
		String updDatetime = request.getParameter("updDatetime");
	
		stdCompinfoLog.setCompId(compId);
		stdCompinfoLog.setUpdDatetime(Timestamp.valueOf(updDatetime));
		List<Map<String,Object>> list = stdCompinfoManager.queryStdCompinfoLogById(page, stdCompinfoLog);
		
		
		List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
		String[] cColunm = {"企业编码","企业名称","企业名称拼音码","企业英文名称","企业简称","简称拼音码","企业类型","地区","企业注册类型","组织机构代码","所属集团","机构电话","机构地址","企业分类","状态","注册资本","企业简介","营业范围","法人姓名","法人身份证号","法人电话","法人手机","联系人姓名","联系人身份证号","联系人职务","联系人电话","联系人手机","联系人邮箱","联系人QQ","营业执照编号","营业执照有效期始","营业执照有效期止","税务登记证号","经营许可证编号","经营许可证有效期始","经营许可证有效期止","企业法人营业执照号","企业法人营业执照号有效期始","企业法人营业执照号有效期止"};
		String[] eColunm = {"comp_code","comp_name","comp_spel_code","comp_name_eng","comp_short_name","comp_short_spel_code","comp_type","area_name","comp_reg_name","org_code","comp_group_name","comp_phone","comp_address","comp_classify","status","reg_cap","comp_profile","biz_scope","legal_rep_name","legal_rep_idnum","legal_rep_telephone","legal_rep_handphone","contact_name","contact_idnum","contact_pos","contact_telephone","contact_handphone","contact_email","contact_qq","biz_lice_no","biz_lice_start_date","biz_lice_end_date","tax_code","indus_code","indus_code_start_date","indus_code_end_date","legal_rep_cert_code","legal_rep_start_date","legal_rep_end_date"};
		for(int i = 0;i<cColunm.length;i++){
			Map<String,Object> map =  new HashMap<String,Object>();
			map.put("colunm", cColunm[i]);//字段名
			map.put("after", !"status".equals(eColunm[i])?list.get(0).get(eColunm[i]):"1".equals(list.get(0).get(eColunm[i]).toString())?"启用":"2".equals(list.get(0).get(eColunm[i]).toString())?"停用":"合并");//修改后数据
			map.put("before", list.size()==1?null:!"status".equals(eColunm[i])?list.get(1).get(eColunm[i]):"1".equals(list.get(1).get(eColunm[i]).toString())?"启用":"2".equals(list.get(1).get(eColunm[i]).toString())?"停用":"合并");//修改前数据
			if("comp_type".equals(eColunm[i])){
				map.put("after", "1".equals(list.get(0).get(eColunm[i]).toString())?"生产企业":"配送企业");
				map.put("before", list.size()==1?null:"1".equals(list.get(1).get(eColunm[i]).toString())?"生产企业":"配送企业");
			}
			if("comp_classify".equals(eColunm[i])){
				map.put("after", Classify.getName(Integer.valueOf(list.get(0).get(eColunm[i]).toString())));
				map.put("before", list.size()==1?null:Classify.getName(Integer.valueOf(list.get(1).get(eColunm[i]).toString())));
			}
			
			if(list.size()==1){
				map.put("red", "1");//是否变红加粗 1是 0 否
			}else{
				//判断修改后与修改前数据是否相等
				if(list.get(0).get(eColunm[i])!=null&&list.get(1).get(eColunm[i])!=null&&!list.get(0).get(eColunm[i]).toString().equals(list.get(1).get(eColunm[i]).toString())){
					map.put("red", "1");
				}else{
					map.put("red", "0");
				}
			}
			mapList.add(map);
		}
		for(int i = 0;i<mapList.size();i++){//把要变红的放在list前面
			if("1".equals(mapList.get(i).get("red"))){
				dataList.add(mapList.get(i));
			}
		}
		for(int i = 0;i<mapList.size();i++){
			if("0".equals(mapList.get(i).get("red"))){
				dataList.add(mapList.get(i));
			}
		}
		request.setAttribute("dataList", dataList);
		
		request.setAttribute("list", list);
		
		return "/base/std/compHisDetail";
	}
	
	/**
	 * 企业库 记录详情前后 
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@RequestMapping("/toDetailDataList")
	@ResponseBody
	public Pagination getDetailDataList(HttpServletRequest request) {
		printParams();
		Pagination page = new Pagination(request);
		StdCompinfoLog stdCompinfoLog = new StdCompinfoLog();

		try {
			String compId = request.getParameter("compId");
			String date1 = request.getParameter("date1");
			stdCompinfoLog.setCompId(compId);
			stdCompinfoLog.setCompCode(date1);
			List<StdCompinfoLog> list = stdCompinfoManager
					.queryStdCompinfoLogByName(page,stdCompinfoLog);
			page.setRecords(list.size());
			page.setRows(list);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error error) {
			error.printStackTrace();
		}
		return page;
	}

	/**
	 * 跳转到合并页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/toCompMergeJsp",method={RequestMethod.GET,RequestMethod.POST})
	public String toCompMergeJsp(HttpServletRequest request){
		printParams();
		String compId = request.getParameter("compIdMerge");
		List<StdCompinfo> list = stdCompinfoManager.queryStdCompinfoByIdForMerge(compId);
		request.setAttribute("list", list.get(0));
		return "/base/std/compMerge";
	}
	
	/**
	 * 企业库 合并页面数据
	 * 
	 * 用来封装json
	 * @param request
	 * @return
	 */
	@RequestMapping("/toMergeDataList")
	@ResponseBody
	public Pagination getMergeDataList(HttpServletRequest request){
		printParams();
		Pagination page = new Pagination(request);
		StdCompinfo stdCompinfo = new StdCompinfo();

		try {
			String compId = request.getParameter("compId");
			String compCode = request.getParameter("compCode");
			String compName = request.getParameter("compName");
			String compType = request.getParameter("compType");
			if(StringUtils.isNotEmpty(compType)){
				stdCompinfo.setCompType(Short.valueOf(compType));
			}
			String areaId = request.getParameter("areaId");
			String compRegId = request.getParameter("compRegId");
			stdCompinfo.setCompId(compId);
			stdCompinfo.setCompCode(compCode);
			stdCompinfo.setCompName(compName);
			stdCompinfo.setAreaId(areaId);
			stdCompinfo.setCompRegId(compRegId);
			page = stdCompinfoManager
					.queryStdCompinfoById(page,stdCompinfo);
			
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error error) {
			error.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 
	 *@category 获取参数打印
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	public void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}
}
